﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KoshiDE;

namespace Solvers.Koshi
{
    public class RungeKuttSolver : KoshiSolver
    {
        public RungeKuttSolver(KoshiTask task)
            : base(task)
        {
            this.Solved = false;
        }

        public override void Solve()
        {
            double Xo, Yo, Y1;
            double h;
            double q1, q2, q4, q3;

            Xo = this.Task.A;
            Yo = this.Task.Y0;
            h = this.Task.H;
            KoshiSolution sol = new KoshiSolution();
            while (Xo <= this.Task.B)
            {
                q1 = h * this.Task.funct(Xo, Yo);
                q2 = h * this.Task.funct(Xo + h / 2.0, Yo + (q1) / 2.0);
                q3 = h * this.Task.funct(Xo + h / 2.0, Yo + (q2) / 2.0);
                q4 = h * this.Task.funct(Xo + h, Yo + q3);
                Y1 = Yo + (1.0 / 6.0) * (q1 + 2.0 * q2 + 2.0 * q3 + q4);
                Yo = Y1;
                sol[Xo] = Yo;

                Xo += h;
            }
            this.Solution = sol;
            this.Solved = true;
        }
    }
}
